package com.cindata

import com.cindata.dictionary.ImportTypeDict
import grails.transaction.Transactional
import org.springframework.security.access.annotation.Secured

import static org.springframework.http.HttpStatus.*

@Secured(['isAuthenticated()'])
@Transactional
class VehicleImportController
{

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def springSecurityService

    def index(Integer max)
    {

        def order = params["order"]
        def sort = params["sort"]
        params.max = Math.min(max ?: 10, 100)
        def importFileType = ImportTypeDict.findByCode("Estate")

        def user = User.findById(springSecurityService.principal.id)

        String sql = " from VehicleImportBatch  e where e.importFileType.id = ${importFileType.id}"
        def accountSql = Account.FilterAccountSql(user)
        if (accountSql)
        {
            sql += " and e.createBy.account.id in ${accountSql}"
        }
//        if (order && sort)
//        {
//            sql += " order by e.${sort} ${order}"
//        }
//        else
//        {
            sql += " order by e.batchNumber desc "
//        }
        def vehicleImportList = VehicleImport.executeQuery(sql, [max: params.max ?: 10, offset: params.offset ?: 0])

        respond VehicleImport.list(params), model: [vehicleImportCount: VehicleImport.count(), vehicleImportBatchList: vehicleImportList, params: params]
    }

    @Transactional
    def searchImport()
    {
        def order = params["order"]
        def sort = params["sort"]
        // def webUtils = WebUtils.retrieveGrailsWebRequest().getSession()
        def queryParam = new HashMap()
        String sql = "from VehicleImport e where 1=1  "
        def user = User.findById(springSecurityService.principal.id)
        def accountSql = Account.FilterAccountSql(user)
        //        if (accountSql)
        //        {
        //            sql += "and e.createBy.account.id in ${accountSql} "
        //        }
        if (params["id"])
        {
            def id = Long.parseLong(params["id"])
            sql += "and e.batch.id = :id "
            queryParam["id"] = id
        }
        if (params["brand"])
        {
            sql += "and e.brand = :brand "
            queryParam["brand"] = params["brand"]
        }
        if (params["evaluateStatue"])
        {
            def evaluateStatue = Long.parseLong(params["evaluateStatue"])
            sql += " and e.evaluateStatue.id = :evaluateStatue "
            queryParam["evaluateStatue"] = evaluateStatue
        }
        if (params["estateStatus"])
        {
            def estateStatus = Long.parseLong(params["estateStatus"])
            sql += " and e.estateStatus.id = :estateStatus "
            queryParam["estateStatus"] = estateStatus
        }
        //执行查询方法
        def count = VehicleImport.executeQuery("select count(e.id) " + sql, queryParam)
        def vehicleImportList
        if (count[0] > 0)
        {
            params.max = params.max ?: 10
            params.offset = params.offset ?: 0
            if (order && sort)
            {
                sql += " order by e.${sort} ${order}"
            }
            else
            {
                sql += " order by e.modifiedDate desc "
            }
            vehicleImportList = VehicleImport.executeQuery(sql, queryParam, [max: params.max ?: 10, offset: params.offset ?: 0])
        }
        if (vehicleImportList != null)
        {
            respond VehicleImport, model: [vehicleImportList: vehicleImportList, vehicleImportCount: count[0], params: params], view: 'index'
        }
        else
        {
            respond VehicleImport, view: 'index'
        }
    }

    def show(VehicleImport vehicleImport)
    {
        respond vehicleImport
    }

    def create()
    {
        respond new VehicleImport(params)
    }

    @Transactional
    def save(VehicleImport vehicleImport)
    {
        if (vehicleImport == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (vehicleImport.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond vehicleImport.errors, view: 'create'
            return
        }

        vehicleImport.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'vehicleImport.label', default: 'VehicleImport'), vehicleImport.id])
                redirect vehicleImport
            }
            '*' { respond vehicleImport, [status: CREATED] }
        }
    }

    def edit(VehicleImport vehicleImport)
    {
        respond vehicleImport
    }

    @Transactional
    def update(VehicleImport vehicleImport)
    {
        if (vehicleImport == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (vehicleImport.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond vehicleImport.errors, view: 'edit'
            return
        }

        vehicleImport.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'vehicleImport.label', default: 'VehicleImport'), vehicleImport.id])
                redirect vehicleImport
            }
            '*' { respond vehicleImport, [status: OK] }
        }
    }

    @Transactional
    def delete(VehicleImport vehicleImport)
    {

        if (vehicleImport == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        vehicleImport.delete flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'vehicleImport.label', default: 'VehicleImport'), vehicleImport.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NO_CONTENT }
        }
    }

    protected void notFound()
    {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'vehicleImport.label', default: 'VehicleImport'), params.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NOT_FOUND }
        }
    }
}
